package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smartfactory.sail.config.OperRecord;
import com.smartfactory.sail.entity.OrderItem;
import com.smartfactory.sail.entity.Plan;
import com.smartfactory.sail.entity.enums.OrderItemStatusEnum;
import com.smartfactory.sail.service.*;
import com.smartfactory.sail.vo.plan.ListUnPlanOrderItem;
import com.smartfactory.sail.vo.plan.PlanDetailResp;
import com.smartfactory.sail.vo.plan.PlanReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-09-05
 */
@RestController
@RequestMapping("/plan")
@Api(tags = "☆☆☆☆☆☆排班计划☆☆☆☆☆☆")
public class PlanController extends ControllerResponse {

    private IOrderItemService iOrderItemService;
    private IPlanService iPlanService;
    private IOrderService iOrderService;
    private IItemService iItemService;
    private IContractService iContractService;

    public PlanController(IOrderItemService iOrderItemService, IPlanService iPlanService, IOrderService iOrderService, IItemService iItemService, IContractService iContractService) {
        this.iOrderItemService = iOrderItemService;
        this.iPlanService = iPlanService;
        this.iOrderService = iOrderService;
        this.iItemService = iItemService;
        this.iContractService = iContractService;
    }

    @ApiOperation(value = "获取所有未排班生产列表", notes = "获取所有未排班生产列表")
    @GetMapping("listUnPlanOrderItem")
    public ApiResponse<List<ListUnPlanOrderItem>> listUnPlanOrderItem() {
        QueryWrapper<OrderItem> query = new QueryWrapper<>();
        query.le("status", OrderItemStatusEnum.DPB.getCode());
        List<OrderItem> list = iOrderItemService.list(query);
        List<ListUnPlanOrderItem> result = new ArrayList<>();
        for (OrderItem orderItem : list) {
            ListUnPlanOrderItem listUnPlanOrderItem = new ListUnPlanOrderItem();
            listUnPlanOrderItem.setOrderCode(iOrderService.getById(orderItem.getOrderId()).getOrderCode());
            listUnPlanOrderItem.setCustName(orderItem.getCustName());
            listUnPlanOrderItem.setItemName(orderItem.getItemName());
            listUnPlanOrderItem.setSize(orderItem.getSize());
            listUnPlanOrderItem.setHourlyCapacity(iItemService.getById(orderItem.getItemId()).getHourlyCapacity());
            listUnPlanOrderItem.setDeliveryDate(orderItem.getDeliveryDate());
            listUnPlanOrderItem.setOrderItemId(orderItem.getId());
            listUnPlanOrderItem.setContractType(iContractService.getById(orderItem.getContractId()).getContractType());
            listUnPlanOrderItem.setStatus(orderItem.getStatus());
            result.add(listUnPlanOrderItem);
        }
        return ok(result);
    }

    @OperRecord(content = "创建排班",level = 1)
    @ApiOperation(value = "创建排班", notes = "创建排班")
    @GetMapping("create/{id}/{hours}/{startDate}/{isUrgentConfirm}")
    public ApiResponse<List<PlanDetailResp>> create(@PathVariable("id") Long id, @PathVariable("hours") Integer hours, @PathVariable("startDate") String startDate, @PathVariable("isUrgentConfirm") boolean isUrgentConfirm) {

        return ok(iPlanService.create(id, hours, startDate, isUrgentConfirm));

    }

    @ApiOperation(value = "预览排班", notes = "预览排班")
    @GetMapping("preview/{id}")
    public ApiResponse<List<PlanDetailResp>> preview(@PathVariable("id") Long id) {

        return ok(iPlanService.preview(id));

    }

    @ApiOperation(value = "获取所有已排班列表", notes = "获取所有已排班列表")
    @GetMapping("listPlanedOrderItem/{deptId}/{startDate}")
    public ApiResponse<List<PlanDetailResp>> listPlanedOrderItem(
            @PathVariable("deptId") long deptId,
            @PathVariable("startDate") String startDate
    ) {
        return ok(iPlanService.listPlanedOrderItem(deptId, startDate, 7));
    }

    @OperRecord(content = "手动调整排班", level = 1)
    @ApiOperation(value = "调整某个产品某天的排班时间（增加或修改）", notes = "调整某个产品某天的排班时间（增加或修改）")
    @PostMapping("rePlan")
    public ApiResponse<List<PlanDetailResp>> rePlan(@RequestBody PlanReq planReq) {
        return ok(iPlanService.rePlan(planReq));
    }

    @ApiOperation(value = "排班预览取消", notes = "排班预览取消")
    @DeleteMapping("cancel/{orderId}/{itemId}")
    public ApiResponse<Integer> cancelPlan(@PathVariable long orderId, @PathVariable long itemId) {
        return ok(iPlanService.cancelPlan(orderId, itemId));
    }

    @ApiOperation(value = "确认排班", notes = "确认排班")
    @PutMapping("confirm/{orderId}/{itemId}")
    public ApiResponse<Object> confirmPlan(@PathVariable long orderId, @PathVariable long itemId) {
        iPlanService.confirmPlan(orderId, itemId);
        return ok();
    }
}
